package com.hdoit.apollo.logical.inventory.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.tennetcn.core.common.utils.CommonUtils;
import com.tennetcn.data.dao.base.impl.SuperService;
import com.tennetcn.data.enums.ModelStatus;
import com.alibaba.druid.util.StringUtils;
import com.hdoit.apollo.logical.inventory.dao.IInventoryStaffDao;
import com.hdoit.apollo.logical.inventory.model.InventoryCollocation;
import com.hdoit.apollo.logical.inventory.model.InventoryShow;
import com.hdoit.apollo.logical.inventory.model.InventoryStaff;
import com.hdoit.apollo.logical.inventory.service.IInventoryCollocationService;
import com.hdoit.apollo.logical.inventory.service.IInventoryShowService;
import com.hdoit.apollo.logical.inventory.service.IInventoryStaffService;
/**
 * @author      tancongzhu
 * @email       
 * @createtime  2018年04月10日 09:44:59
 * @comment
 */
@Component
public class InventoryStaffServiceImpl extends SuperService<InventoryStaff> implements IInventoryStaffService{

	@Autowired
	private IInventoryShowService inventoryShowService;
	@Autowired
	private IInventoryCollocationService inventoryCollocationService;
	@Autowired
	private IInventoryStaffDao inventoryStaffDao;
	
	@Override
	public boolean applyInventoryStaff(InventoryStaff inventoryStaff, List<InventoryShow> inventoryShows) {
		List<InventoryCollocation> inventoryCollocations = new ArrayList<InventoryCollocation>();
		if(super.applyChange(inventoryStaff)){
			if(inventoryStaff.getModelStatus() == ModelStatus.add){
				for (InventoryShow inventoryShow : inventoryShows) {
					if (StringUtils.isEmpty(inventoryShow.getShowId())) {
						inventoryShow.setShowId(CommonUtils.getKeyId());
					}
					inventoryShow.setInventoryStaffId(inventoryStaff.getInventoryStaffId());
					inventoryShow.setModelStatus(ModelStatus.add);
					
					for (InventoryCollocation inventoryCollocation : inventoryShow.getInventoryCollocations()) {
						if (StringUtils.isEmpty(inventoryCollocation.getInventoryCollocationId())) {
							inventoryCollocation.setInventoryCollocationId(CommonUtils.getKeyId());
						}
						inventoryCollocation.setModelStatus(ModelStatus.add);
						inventoryCollocations.add(inventoryCollocation);
						
					}
				}

			}else{
				InventoryStaff inventoryStaffPan = inventoryStaffDao.queryModelById(inventoryStaff.getInventoryStaffId());
				for (InventoryShow inventoryShow : inventoryStaffPan.getInventoryShows()) {
					inventoryCollocationService.deleteInventoryCollocation(inventoryShow.getShowId());
				}
				if(inventoryShowService.deleteInventoryShow(inventoryStaff.getInventoryStaffId())) {
					for (InventoryShow inventoryShow : inventoryShows) {
						InventoryShow inventoryShowPan = inventoryShowService.queryModel(inventoryShow.getShowId());
						if(inventoryShowPan==null){
							inventoryShow.setInventoryStaffId(inventoryStaff.getInventoryStaffId());
							inventoryShow.setModelStatus(ModelStatus.add);
							
							for (InventoryCollocation inventoryCollocation : inventoryShow.getInventoryCollocations()) {
								inventoryCollocation.setModelStatus(ModelStatus.add);
								inventoryCollocations.add(inventoryCollocation);
								
							}
						}else {
							inventoryShow.setModelStatus(ModelStatus.update);
							for (InventoryCollocation inventoryCollocation : inventoryShow.getInventoryCollocations()) {
								InventoryCollocation inventoryCollocationPan=inventoryCollocationService.queryModel(inventoryCollocation.getInventoryCollocationId());
								if(inventoryCollocationPan==null){
									inventoryCollocation.setModelStatus(ModelStatus.add);
									inventoryCollocations.add(inventoryCollocation);
								}else {
									inventoryCollocation.setModelStatus(ModelStatus.update);
									inventoryCollocations.add(inventoryCollocation);
								}
								
							}
						}
	
					}
				}

			}

			return inventoryShowService.applyChanges(inventoryShows)&&inventoryCollocationService.applyChanges(inventoryCollocations);
		}
		return false;
	}

	@Override
	public InventoryStaff queryModelById(String inventoryStaffId) {
		return inventoryStaffDao.queryModelById(inventoryStaffId);
	}

}